<?php
/**
 * upload.php
 * @author ranuandra
 *
 * 10 Agu 2011 10:48:59
 *
 * 
 */

class Controller_Upload extends Kohana_Polreg
{
	
	public $auth_required = array('login', 'admin'); 
	
	public function action_index()
	{
		$form = array();
		if(isset($_POST['bulan']) && isset($_POST['tahun']) )
		{
			$form = Request::current()->post();
		}
		else 
		{
			$form['bulan'] = '';
			$form['tahun'] = '';
		}
		//$container = View::factory('upload/form');
		//$container->title = "Upload File";
		//$this->template->container = $container;
		$upload = ORM::factory('upload')->getData($form);
		
		//print_r($upload);
		//exit;
		
		//$upload = ORM::factory('upload')->find_all();
		
		$container = View::factory('upload/index');
		$container->datas = $upload;
		$container->title = "Data Upload File";
		$container->bulan = array('','Januari', 'Februari', 'Maret', 'April', 'Mei', 'Juni', 'Juli', 'Agustus', 'September', 'Oktober', 'November', 'Desember');
		$container->form = $form;
		if(Request::current()->is_ajax() == $this->request)
		{
			$container->ajax = 1;
			echo $container;
			exit;
		}
		else 
		{	$container->ajax = 0;
			$menu = View::factory('module/menu');
			$this->template->menu = $menu;
			$this->template->container = $container;
		}
		
	}
	
	public function action_new()
	{
		$container = View::factory('upload/form');
		$container->title = "Upload File";
		if(Request::current()->is_ajax() == $this->request)
		{
			$container->ajax = 1;
			echo $container;
			exit;
		}
		else 
		{	$container->ajax = 0;
			$menu = View::factory('module/menu');
			$this->template->menu = $menu;
			$this->template->container = $container;
		}		
	}
	
	public function action_save()
	{
		$post = Validation::factory($_FILES);
		$post->rule('fname', 'Upload::type', array(':value', array('xls')));
		if($post->check())
		{
			$fileName =  str_replace(' ', '_', $post['fname']['name']);
			//echo $post['fname']['name'];
			if(!file_exists(DOCROOT.'assets\file\\'.$fileName)) // jika file tidak ada maka upload file
			{
				Upload::save($post['fname'],$fileName, DOCROOT.'assets\file\\', 777);
				
			}
			
			include_once Kohana::find_file('vendor', 'Excel/reader.php',  array(), array());
		
			$data = new Spreadsheet_Excel_Reader();
			$data->setOutputEncoding('CP1251');
			$data->read(DOCROOT.'assets\file\\'.$fileName);
			
			error_reporting(E_ALL ^ E_NOTICE);
			/*
			 * 1 area
			 * 2 nama
			 * 3 alamat
			 * 4 model
			 * 5 merek
			 * 6 type
			 * 7 cc
			 * 8 tahun pembuatan
			 * 9 noka
			 * 10 nosin
			 * 11 tgl daftar
			 * 
			 */
			
			$dtupload = ORM::factory('upload')->where('fname', '=', $fileName )->find();
			if(!$dtupload->id)
			{
				$orupload = ORM::factory('upload');
				$orupload->fname = $fileName;
				$orupload->tgl_upload = date("Y-m-d H:m:s"); 
				$orupload->user_id = Auth_ORM::instance()->get_user()->id;
				$orupload->save();
				
				$uploadid = $orupload->id;
				
				
			}//file
			else 
			{
				$uploadid = $dtupload->id;
			}
			
				
			$xldatas = $this->xlstoarry($data);
			
			//print_r($xldatas);
			//exit;
			if($xldatas[0]['noka']!="")
			{
				
				$polreg = new Model_Polreg();
				$result = ORM::factory('polreg')->transaksi($xldatas, $uploadid);
				//$result = $polreg->create($xldatas, $uploadid);
				//$datas = new Model_Polreg();
				 ORM::factory('polreg')->create_data($result, $uploadid);
					  
				$container = View::factory('upload/data');
				$container->datas = $xldatas;
				$data->title = $fileName;
				$menu = View::factory('module/menu');
				$this->template->menu = $menu;
				$this->template->container = $container;
			}
			else 
			{
				$message = "Format file tidak sesuai silahkan di cek lagi";
			}

		}//if check

		if(isset($_POST['bulan']) && isset($_POST['tahun']) )
		{
			$form = Request::current()->post();
		}
		else 
		{
			$form['bulan'] = '';
			$form['tahun'] = '';
		}
		
		$upload = ORM::factory('upload')->getData($form);
		//$upload = ORM::factory('upload')->find_all();
		
		$container = View::factory('upload/index');
		$container->datas = $upload;
		$container->title = "Data Upload File";
		$container->message = $message;
		$container->form = $form;
		$container->bulan = array('','Januari', 'Februari', 'Maret', 'April', 'Mei', 'Juni', 'Juli', 'Agustus', 'September', 'Oktober', 'November', 'Desember');
		if(Request::current()->is_ajax() == $this->request)
		{
			$container->ajax = 1;
			echo $container;
			exit;
		}
		else 
		{	$container->ajax = 0;
			$menu = View::factory('module/menu');
			$this->template->menu = $menu;
			$this->template->container = $container;
		}
	
		
	}
	
	public function action_delete($id)
	{
		$upload = ORM::factory('upload')->where('id', '=', $id)->find();
		$fileName = $upload->fname;
		
		if(is_file(DOCROOT.'assets\file\\'.$fileName))
		{		
			try 
			{
				chown(DOCROOT.'assets\file\\'.$fileName,0666);
				chmod(DOCROOT.'assets\file\\'.$fileName,0777);
				//fclose(DOCROOT.'assets\file\\'.$fileName);  
				if(unlink(DOCROOT.'assets\file\\'.$fileName))
				{
					$message = "file berhasil dihapus";

					
					//print_r($file);
					
					
				}
				else 
				{
					$message = "file gagal dihapus";
				}

			}
			catch(Exception $e)
			{
				$message = $e->getMessage();
			}
		}
		else
		{
			$message = "File tidak ditemukan";
		}
		
		DB::delete('polregs')->where('upload_id', '=', $id)->execute();
		$file = ORM::factory('upload', $id);
		$file->delete();	

		if(isset($_POST['bulan']) && isset($_POST['tahun']) )
		{
			$form = Request::current()->post();
		}
		else 
		{
			$form['bulan'] = '';
			$form['tahun'] = '';
		}
		
		$upload = ORM::factory('upload')->getData($form);
		
		$container = View::factory('upload/index');
		$container->datas = $upload;
		$container->title = "Data Upload File";
		$container->form = $form;
		$container->bulan = array('','Januari', 'Februari', 'Maret', 'April', 'Mei', 'Juni', 'Juli', 'Agustus', 'September', 'Oktober', 'November', 'Desember');
		if(isset($message))
		{
			$container->message = $message;
		}
		if(Request::current()->is_ajax() == $this->request)
		{
			$container->ajax = 1;
			echo $container;
			exit;
		}
		else 
		{	$container->ajax = 0;
			$menu = View::factory('module/menu');
			$this->template->menu = $menu;
			$this->template->container = $container;
		}
		
		
	}
}